package edu.byui.chat;

import java.util.GregorianCalendar;

public class MySQLConversion {
	@SuppressWarnings("static-access")
	public static String gregorToDateTime(GregorianCalendar cal) {
		String result = new String();
		// The format is 'YYYY-MM-DD hh:mm:ss'
		result = Integer.toString(cal.get(cal.YEAR));
		result += "-" + Integer.toString((cal.get(cal.MONTH)+1));
		result += "-" + Integer.toString(cal.get(cal.DAY_OF_MONTH));
		result += " " + Integer.toString(cal.get(cal.HOUR_OF_DAY));
		result += ":" + Integer.toString(cal.get(cal.MINUTE));
		result += ":" + Integer.toString(cal.get(cal.SECOND));
		result += "." + Integer.toString(cal.get(cal.MILLISECOND));

		return result;
	}

	@SuppressWarnings("static-access")
	public static GregorianCalendar dateTimeToGregor(String in) {
		GregorianCalendar cal = new GregorianCalendar();
		String[] slices = in.split("[- :.]");
		cal.set(cal.YEAR, Integer.parseInt(slices[0]));
		cal.set(cal.MONTH, Integer.parseInt(slices[1])-1);
		cal.set(cal.DAY_OF_MONTH, Integer.parseInt(slices[2]));
		cal.set(cal.HOUR_OF_DAY, Integer.parseInt(slices[3]));
		cal.set(cal.MINUTE, Integer.parseInt(slices[4]));
		cal.set(cal.SECOND, Integer.parseInt(slices[5]));
		cal.set(cal.MILLISECOND, Integer.parseInt(slices[6]));
		return cal;
	}

	public static void main(String args[]) {
		System.out.println(gregorToDateTime(new GregorianCalendar()));
		System.out.println(gregorToDateTime(dateTimeToGregor(gregorToDateTime(new GregorianCalendar()))));
	}
}
